import 'package:bxultimate/resources/assets.dart';
import 'package:bxultimate/widgets/mk_load_image.dart';
import 'package:bxultimate/widgets/mk_page.dart';
import 'package:bxultimate/widgets/mk_scroll_view.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'controller.dart';

class OrganizationSelectUserPage extends StatefulWidget {
  const OrganizationSelectUserPage({Key? key}) : super(key: key);

  @override
  State<OrganizationSelectUserPage> createState() =>
      _OrganizationSelectUserPageState();
}

class _OrganizationSelectUserPageState
    extends State<OrganizationSelectUserPage> {
  final controller = Get.find<OrganizationSelectUserController>();
  final state = Get
      .find<OrganizationSelectUserController>()
      .state;

  @override
  Widget build(BuildContext context) {
    return MkPage(
        child: Scaffold(
          backgroundColor: Colors.transparent,
          appBar: TDNavBar(
            backgroundColor: Colors.transparent,
            title: '选择人员',
          ),
          body: Obx(()=>MkScrollView(
            bottomButton: TDButton(
              text: '确认选择',
              shape: TDButtonShape.circle,
              height: 48,
              onTap: controller.handleSelect,
              isBlock: true,
              theme: TDButtonTheme.primary,
            ),
            children: [
              TDCheckboxGroupContainer(
                selectIds: controller.checkIds,
                passThrough: false,
                onCheckBoxGroupChange: (ids) {
                  controller.checkIds = ids;
                },
                controller: controller.checkboxGroupController,
                child: ListView.builder(
                  padding: const EdgeInsets.all(0),
                  shrinkWrap: true,
                  physics: const NeverScrollableScrollPhysics(),
                  itemBuilder: (context, index) {
                    var title = '';
                    if (index == 0) {
                      title = '全选';
                      return SizedBox(
                        height: 56,
                        child: TDCheckbox(
                          id: 'index:$index',
                          title: title,
                          customIconBuilder: (context, checked) {
                            var length = controller.checkboxGroupController.allChecked().length - (controller.checkboxGroupController.checked('index:0') ? 1 : 0);
                            var allCheck = state.userList.length - 1 == length;
                            var halfSelected =
                                controller.checkboxGroupController != null
                                    && !allCheck
                                    && length > 0;
                            return getAllIcon(allCheck, halfSelected);
                          },
                          onCheckBoxChanged: (checked){
                            if (checked) {
                              controller.checkboxGroupController.toggleAll(true);
                            } else {
                              controller.checkboxGroupController.toggleAll(false);
                            }
                          },
                        ),
                      );
                    } else {
                      return TDCheckbox(
                        id: 'index:$index',
                        title: state.userList[index].name,
                        subTitle: state.userList[index].idcard,
                        onCheckBoxChanged: (checked){
                          var length = controller.checkboxGroupController.allChecked().length - (controller.checkboxGroupController.checked('index:0') ? 1 : 0);
                          var allCheck = state.userList.length - 1 == length;
                          var halfSelected =
                              controller != null
                                  && !allCheck
                                  && length > 0;
                          controller.checkboxGroupController.toggle('index:0', allCheck);
                          getAllIcon(allCheck, halfSelected);
                        },
                      );
                    }
                  },
                  itemCount: state.userList.length,
                ),
              )
            ],
          )),
        ));
  }

  Widget getAllIcon(bool checked, bool halfSelected) {
    return Icon(
        checked ? TDIcons.check_circle_filled : halfSelected ? TDIcons.minus_circle_filled : TDIcons.circle,
        size: 24,
        color: (checked || halfSelected) ? TDTheme.of(context).brandNormalColor : TDTheme.of(context).grayColor4
    );
  }

  @override
  void dispose() {
    Get.delete<OrganizationSelectUserController>();
    super.dispose();
  }
}
